'use strict';

const db = uniCloud.database();
const dbCmd = db.command;
const $ = dbCmd.aggregate;

const db_name_user = 'star-community-user';
const db_name_post = 'star-community-post';
const db_name_comment = 'star-community-comment';
const db_user = db.collection(db_name_user);
const db_post = db.collection(db_name_post);
const db_comment = db.collection(db_name_comment);

exports.main = async (event, context) => {
	const flag = event.flag;

	if (flag === 0) { // 更改用户属性count
		const {
			id,
			careIncCount,
			fansIncCount,
			likeIncCount,
			collectIncCount,
		} = event.data;
		return await db_user.where({
			id
		}).update({
			careCount: dbCmd.inc(careIncCount || 0),
			fansCount: dbCmd.inc(fansIncCount || 0),
			likeCount: dbCmd.inc(likeIncCount || 0),
			collectCount: dbCmd.inc(collectIncCount || 0),
		});
	} else if (flag === 1) { // 更改帖子属性count
		const {
			id,
			shareIncCount,
			viewIncCount,
			commentIncCount,
			likeIncCount,
			reportIncCount,
		} = event.data;
		return await db_post.where({
			id
		}).update({
			shareCount: dbCmd.inc(shareIncCount || 0),
			viewCount: dbCmd.inc(viewIncCount || 0),
			commentCount: dbCmd.inc(commentIncCount || 0),
			likeCount: dbCmd.inc(likeIncCount || 0),
			reportCount: dbCmd.inc(reportIncCount || 0),
		});
	} else if (flag === 2) { // 更改评论属性count
		const {
			id,
			commentIncCount,
			likeIncCount,
			reportIncCount,
		} = event.data;
		return await db_comment.where({
			id
		}).update({
			commentCount: dbCmd.inc(commentIncCount || 0),
			likeCount: dbCmd.inc(likeIncCount || 0),
			reportCount: dbCmd.inc(reportIncCount || 0),
		});
	}
};